package app.stock.client;

import java.text.ParseException;

import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DateFormatter;
import javax.swing.text.MaskFormatter;
import javax.swing.text.PlainDocument;

import app.common.Util;

public class ClientUtil {
	public static DateFormatter getDefaultDateFormatter() {
		DateFormatter df = new DateFormatter(Util.getDefaultDateFormat());
		return df;
	}

	public static MaskFormatter getDefaultIntegerFormatter() {
		MaskFormatter mf = null;
		try {
			mf = new MaskFormatter("######");
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return mf;
	}

	public static MaskFormatter getDefaultFloatFormatter() {
		MaskFormatter mf = null;
		try {
			mf = new MaskFormatter("######.##");
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return mf;
	}

	public static PlainDocument getDefaultNumericDocument() {
		return new NumericDocument();
	}
}

class NumericDocument extends PlainDocument {
	public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
		try {
			if (!".".equals(str)) {
				Double.parseDouble(str);
			}
		} catch (NumberFormatException e) {
			return;
		}
		super.insertString(offs, str, a);
	}
}